﻿#region License
// Copyright 2008 Doug Mayer - http://code.google.com/p/nfaker
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System.Text.RegularExpressions;

namespace NFaker
{
	public class FakeInternet
	{
		private static readonly Regex nonWordMatch = new Regex(@"\W");

		internal FakeInternet() { }

		public string DomainName()
		{
			return string.Join(".", new[] { DomainWord(), DomainSuffix() });
		}

		public string DomainSuffix()
		{
			return domainSuffixes.Random();
		}

		public string DomainWord()
		{
			return nonWordMatch.Replace(Fake.Company.Name().Split(' ')[0], "").ToLowerInvariant();
		}

		public string Email()
		{
			return Email(Fake.Name.FullName());
		}

		public string Email(string name)
		{
			return string.Join("@", new[] { UserName(name), DomainName() });
		}

		public string FreeEmail()
		{
			return FreeEmail(Fake.Name.FullName());
		}

		public string FreeEmail(string name)
		{
			return string.Join("@", new[] { UserName(name), freeEmailDomains.Random() });
		}

		public string UserName()
		{
			return UserName(Fake.Name.FullName());
		}

		public string UserName(string name)
		{
			string separator = new[] { "", ".", "_" }.Random();

			return string.Join(separator, name.Split(' ').ShuffleInPlace()).ToLowerInvariant();
		}

		#region Private value arrays

		private static readonly string[] domainSuffixes = new[]
		                                                  	{
		                                                  		"co.uk", "com", "us", "uk", "ca", "biz", "info", "name"
		                                                  	};

		private static readonly string[] freeEmailDomains = new[]
		                                                    	{
		                                                    		"gmail.com", "yahoo.com", "hotmail.com"
		                                                    	};

		#endregion
	}
}